/*
Copyright (C) 2008 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

/**
 * @param {String} description The description field from the Google News RSS
 *     feed. All of the relevant information about the news item can be 
 *     extracted from this field.
 * @constructor
 */
function NewsItem(description) {
  this.parseDescription_(description);
}

/**
 * Matches on the description field of a news article.
 * The following imformation is extracted from the description text: 
 * <pre>
 *   $1: The article link
 *   $2: The article title
 *   $3: The article source
 *   $4: How long ago the article was published
 *   $5: The article snippet
 *   $6: The link to more articles.
 *   $7: The text for the link to more articles.
 * </pre>
 * @type {RegExp}
 */
NewsItem.DESCRIPTION_RE = new RegExp(
    '<img alt=.*?<a\\s+href="(.*?)"' + // Article link
    '.*?' +
    '<b>(.*?)</b>' + // Article title
    '.*?' +
    '<font color=#6f6f6f>(.*?)</font>' + // Article source
    '.*?' +
    '<nobr>(.*?)</nobr>' + // How long ago articled was published
    '.*?' +
    '</font><br><font size=-1>(.*?)</font>' + // Article snippet
    '.*?' +
    '<a class=p href=(.*?)>' + // More link
    '.*?' +
    '<b>(.*?)</b>' // all articles text
    );

/**
 * Matches on the description field of a news article. Extracts information
 * about the article image.
 * The following imformation is extracted from the description text: 
 * <pre>
 *   $1: The link to the source news agency
 *   $2: The the source of the image
 *   $3: The name of the news agency (the image credit)
 * </pre>
 * @type {RegExp}
 */
NewsItem.IMAGE_RE = new RegExp(
    '<a\\s+href="(.*?)"' +
    '.*?' +
    '<img src=(.*?)\\s' +
    '.*?' +
    '<font size=-2>(.*?)</font>'
    ); 

/**
 * Parses the description field and extrace all relevant information from it,
 * and assigns the relevant information to public instance variables.
 * @param {String} description The description field of the News RSS feed.
 */
NewsItem.prototype.parseDescription_ = function(description) {
  this.isValid = true;

  var matches = description.match(NewsItem.DESCRIPTION_RE);
  if (!matches) {
    this.isValid = false;
    return;
  }

  var html = "<html><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">";
  html += "<head><style type=\"text/css\">body {font-size: 12px;}</style></head><body>";
  html += description.replace(/<embed.*?>/g, '');
  html += "</body></html>";
  this.description = html;

  this.link = matches[1];
  this.title = this.replaceHtml_(matches[2]);
  this.source = this.replaceHtml_(matches[3].replace('&nbsp;-', ''));
  this.ago = matches[4];
  this.snippet = matches[5];
  this.moreLink = matches[6];
  this.moreText = matches[7];
  
  matches = description.match(NewsItem.IMAGE_RE);
  if (matches) {
    this.imageLink = matches[1];
    this.imageSrc = matches[2];
    this.imageCredit = matches[3];
  }
};

/**
 * @param {String} text
 * @return {String} The contents of 'text' with the html entities replaced with
 *     their literal characters. 
 */
NewsItem.prototype.replaceHtml_ = function(text) {
  var retText = text.replace(/\&#39;/g, '\'');
  retText = retText.replace(/\&quot;/g, '"');
  
  return retText;
};
